import { getBooks, getTotal } from '@/api/book'
const state = {
  books: [],
  total: 0
}

const mutations = {
  SET_BOOKS(state, books) {
    state.books = books
  },
  SET_TOTAL(state, total) {
    state.total = total
  }
}

const actions = {
  async getBooks({ commit }, { pageSize, pageNo }) {
    const { code, data: books } = await getBooks({
      pageSize,
      pageNo
    })
    if (code === 20000) {
      commit('SET_BOOKS', books)
    }
  },

  async getTotal({ commit }) {
    const { code, data: total } = await getTotal()
    if (code === 20000) {
      commit('SET_TOTAL', total)
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
